#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include<ctime>

using namespace std;

void handler(int signo)
{
    cout<<"get a sig: "<<signo<<endl;
    sleep(5);

    pid_t rid;
    while((rid=waitpid(-1, nullptr, WNOHANG))>0)
    {
        cout<<"child process quit!!!: "<<rid<<endl;
        sleep(1);
    }
}


int main()
{   
    srand(time(nullptr));
    //signal(17,handler);
    signal(17,SIG_IGN); //对17号进程忽略，那么这个子进程退出时不会僵尸，会直接释放

    for(int i=0;i<10;i++)
    {   
        //sleep(rand()%5);
        pid_t id = fork();
        if (id == 0)
        {
            int cnt = 5;
            while (cnt)
            {
                cout << "i am child process: " << getpid() << " " << getppid() << endl;
                cnt--;

                sleep(1);
            }

            exit(0);
        }
    }
    
    // father
    while (true)
    {
        cout << "i am father process: " << getpid() << endl;
        sleep(1);
    }

    return 0;
}